var _tools = require("../../../utils/tools.js"), _tools2 = _interopRequireDefault(_tools);

function _interopRequireDefault(t) {
    return t && t.__esModule ? t : {
        default: t
    };
}

var that, app = getApp();
let interstitialAd = null;

Page({
    data: {
        currentSwiper: 0,
        d: "29",
        h: "00",
        m: "00",
        s: "10",
        request: 1,
        setTimeself: "",
        time: 0,
        loading: !1,
        interstitialAd: ""
    },
    onLoad: function() {
        that = this;
    },
    adError: function(t) {
        console.log(1111111), console.log(t);
    },
    a: function() {},
    onReady: function() {
        wx.setNavigationBarColor({
            frontColor: "#000000",
            backgroundColor: "#000000"
        });
        var t = {
            action: "category",
            contr: "goods",
            token: wx.getStorageSync("token")
        };
        _tools2.default.request(t, function(t) {
            that.setData(t.info), that.loadGoods(), that.clearTime(), that.countdown(), wx.createInterstitialAd && that.data.config.screen_id && (that.data.interstitialAd = wx.createInterstitialAd({
                adUnitId: that.data.config.screen_id
            }), that.data.interstitialAd.onError(function(t) {
                console.log(t);
            }), that.data.interstitialAd.show().catch(function(t) {
                console.error(t);
            }));
        });
    },
    onShow: function() {
        if (wx.createInterstitialAd) {
            interstitialAd = wx.createInterstitialAd({
              adUnitId: 'adunit-42e141bfcfdcc315'
            })
            interstitialAd.onLoad(() => {})
            interstitialAd.onError((err) => {})
            interstitialAd.onClose(() => {})
          };
          
          // 在适合的场景显示插屏广告
          if (interstitialAd) {
            interstitialAd.show().catch((err) => {
              console.error(err)
            })
          };
    },
    clearTime: function() {
        clearTimeout(that.data.setTimeself);
    },
    onUnload: function() {
        this.clearTime();
    },
    countdown: function() {
        if (null != this.data.bargain_goods && this.data.bargain_goods) {
            for (var t in this.data.time++, this.data.bargain_goods) {
                var a = parseInt(that.data.bargain_goods[t].surplus_time) - this.data.time;
                if (that.data.bargain_goods[t].d = "00", that.data.bargain_goods[t].h = "00", that.data.bargain_goods[t].m = "00", 
                that.data.bargain_goods[t].s = "00", 0 < a) {
                    var o = parseInt(a / 60 / 60 / 24, 10), e = Math.floor(a / 60 / 60 % 24), n = Math.floor(a / 60 % 60), i = Math.floor(a % 60);
                    e < 10 && (e = "0" + e), o < 10 && (o = "0" + o), n < 10 && (n = "0" + n), i < 10 && (i = "0" + i), 
                    that.data.bargain_goods[t].d = o, that.data.bargain_goods[t].h = e, that.data.bargain_goods[t].m = n, 
                    that.data.bargain_goods[t].s = i;
                }
            }
            that.data.setTimeself = setTimeout(function() {
                that.setData({
                    bargain_goods: that.data.bargain_goods
                }), that.countdown();
            }, 1e3);
        } else clearTimeout(that.data.setTimeself);
    },
    loadGoods: function() {
        var t = {
            action: "goods_list",
            contr: "goods",
            token: wx.getStorageSync("token"),
            category_id: this.data.category[this.data.currentSwiper].id
        };
        _tools2.default.request(t, function(t) {
            that.setData(t.info);
        });
    },
    selSwiper: function(t) {
        this.setData({
            currentSwiper: t.currentTarget.dataset.index
        }), this.loadGoods();
    },
    launch: function(t) {
        var a = t.currentTarget.dataset.id;
        if (1 == this.data.request) {
            this.data.request = 2;
            var o = {
                action: "launch",
                contr: "help",
                token: wx.getStorageSync("token"),
                goods_id: a
            };
            _tools2.default.request(o, function(t) {
                that.data.request = 1, 2 == t.status ? wx.showModal({
                    title: "提示",
                    mask: !0,
                    content: t.info,
                    showCancel: !1
                }) : wx.reLaunch({
                    url: "/bh_bargain/pages/bargain/bargain?id=" + t.info.id
                });
            });
        }
    },
    swiperChange: function(t) {
        this.setData({
            currentSwiper: t.detail.current
        }), this.loadGoods();
    },
    gotoRule: function() {
        wx.navigateTo({
            url: "/bh_bargain/pages/rule/rule?type=1"
        });
    },
    gotoBargain: function(t) {
        var a = t.currentTarget.dataset.id;
        wx.navigateTo({
            url: "/bh_bargain/pages/bargain/bargain?id=" + a
        });
    },
    gotoAll: function() {
        wx.navigateTo({
            url: "/bh_bargain/pages/myBargain/myBargain"
        });
    },
    gotoGoods: function(t) {
        var a = t.currentTarget.dataset.id;
        wx.navigateTo({
            url: "/bh_bargain/pages/goods/goods?id=" + a
        });
    },
    getUserInfo: function(a) {
        if ("getUserInfo:ok" == a.detail.errMsg) {
            var t = {
                action: "register",
                contr: "my",
                token: wx.getStorageSync("token"),
                encryptedData: a.detail.encryptedData,
                iv: a.detail.iv
            };
            _tools2.default.request(t, function(t) {
                that.launch(a);
            });
        }
    },
    onShareAppMessage: function() {
        return {
            title: this.data.share.text,
            imageUrl: this.data.share.images,
            path: "bh_bargain/pages/index/index?parent_id=" + this.data.share.member_id
        };
    }
});